package com.lin.controller;

import com.lin.pojo.User;
import com.lin.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author 卡丘z
 */
@Controller
public class HelloController {

    private final UserService userService;

    public HelloController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("login")
    public String tologin(User user,Model model){
        model.addAttribute("user",user);
        return "login";
    }
    @PostMapping("/login")
    public String login(User user,Model model){
       List<User> userList = userService.list(null);
       String name = user.getUsername();
       String password = user.getPassword();
       for(User u: userList){
           if(name.equals(u.getUsername())&&password.equals(u.getPassword())){
               model.addAttribute("user",user);
               return "success";
           }
           else{
               model.addAttribute("message","用户名或密码错误");
               return "login";
           }
       }
        return "login";
    }

    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("message","hello");
        return "login";
    }
}
